using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Common;
using RMAS.DAL;

namespace RMAS.DAL
{
    /// <summary>
    /// Summary description for QT_ITEMINOUT.
     /// </summary>
	public class QT_ITEMINOUTDao
    {
	    private string _SQLSelectByKey;
	    private string _SQLUpdate;
	    private string _SQLInsert;
	    private string _SQLDelete;
	    private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion
        
        #region Constructor

        public QT_ITEMINOUTDao()
        {
            _dbConnection = null;
		    InitSqlQuery();
        }

        public QT_ITEMINOUTDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
       {
            string query;

            query = @"SELECT ItemID, ItemName, ItemType, ItemObject, ItemNotes FROM QT_ITEMINOUT WHERE (ItemID = @ItemID)";
	        _SQLSelectByKey = query;

            query = @"UPDATE QT_ITEMINOUT SET ItemName = @ItemName, ItemType = @ItemType, ItemObject = @ItemObject, ItemNotes = @ItemNotes WHERE (ItemID = @ItemID)";
	        _SQLUpdate = query;

            query = @"INSERT INTO QT_ITEMINOUT VALUES (@ItemID, @ItemName, @ItemType, @ItemObject, @ItemNotes)";
	        _SQLInsert = query;

            query = @"DELETE FROM QT_ITEMINOUT WHERE (ItemID = @ItemID)";
	        _SQLDelete = query;

            query = @"SELECT ItemID, ItemName, ItemType, ItemObject, ItemNotes FROM QT_ITEMINOUT";
	        _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

				if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(QT_ITEMINOUT obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                         DalTools.addDbDataParameter(command, "ItemID", obj.ItemID, DbType.Int32);
                         DalTools.addDbDataParameter(command, "ItemName", obj.ItemName, DbType.String);
         DalTools.addDbDataParameter(command, "ItemType", obj.ItemType, DbType.String);
         DalTools.addDbDataParameter(command, "ItemObject", obj.ItemObject, DbType.String);
         DalTools.addDbDataParameter(command, "ItemNotes", obj.ItemNotes, DbType.String);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(QT_ITEMINOUT obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                         DalTools.addDbDataParameter(command, "ItemID", obj.ItemID, DbType.Int32);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(QT_ITEMINOUT obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(QT_ITEMINOUT obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties
     
        private QT_ITEMINOUTList SelectListByCommand(IDbCommand command)
        {
            QT_ITEMINOUTList list = new QT_ITEMINOUTList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
		QT_ITEMINOUT obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new QT_ITEMINOUT();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
		
        private QT_ITEMINOUTList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            QT_ITEMINOUTList list = new QT_ITEMINOUTList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				QT_ITEMINOUT obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
				command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new QT_ITEMINOUT();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	
        private void ReadProperties(QT_ITEMINOUT obj, IDataReader reader)
        {
            try
            {
                                if(!(reader["ItemID"] is DBNull))
		{
			obj.ItemID = (Int32)reader["ItemID"];
		}
                if(!(reader["ItemName"] is DBNull))
		{
			obj.ItemName = (string)reader["ItemName"];
		}
                if(!(reader["ItemType"] is DBNull))
		{
			obj.ItemType = (string)reader["ItemType"];
		}
                if(!(reader["ItemObject"] is DBNull))
		{
			obj.ItemObject = (string)reader["ItemObject"];
		}
                if(!(reader["ItemNotes"] is DBNull))
		{
			obj.ItemNotes = (string)reader["ItemNotes"];
		}
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion
      
        public QT_ITEMINOUT SelectByKey(Int32 ItemID)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "ItemID", ItemID, DbType.Int32);
      	    QT_ITEMINOUTList list =  SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public QT_ITEMINOUT SelectByKey(Int32 ItemID, IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "ItemID", ItemID, DbType.Int32);
      	    QT_ITEMINOUTList list =  SelectListByCommand(command, trans);
			
            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }
		
        public QT_ITEMINOUTList SelectAll()
        {
            IDbCommand command  = _dbConnection.CreateCommand();
           
            command.CommandText = _SQLSelectAll;
      	    QT_ITEMINOUTList list =  SelectListByCommand(command);
	        return list;
        }
	public QT_ITEMINOUTList SelectAll(IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
           
            command.CommandText = _SQLSelectAll;
      	    QT_ITEMINOUTList list =  SelectListByCommand(command, trans);
	        return list;
        }
         public QT_ITEMINOUTList SelectByCommand(string Command)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = Command;
      	    QT_ITEMINOUTList list =  SelectListByCommand(command);
	        return list;
        }

        public QT_ITEMINOUTList SelectByCondition(QT_ITEMINOUT obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM QT_ITEMINOUT " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            QT_ITEMINOUTList list = SelectListByCommand(command, trans);
            return list;
        }
		
		 public QT_ITEMINOUTList SelectByCondition(QT_ITEMINOUT obj)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM QT_ITEMINOUT " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            QT_ITEMINOUTList list = SelectListByCommand(command);
            return list;
        }
        private void DeleteByCondition(QT_ITEMINOUT obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM QT_ITEMINOUT " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(QT_ITEMINOUT obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            	if (obj.IsSetItemID)
	{
    	    s += " AND ItemID = @ItemID ";
	}
	if (obj.IsSetItemName)
	{
    	    s += " AND ItemName = @ItemName ";
	}
	if (obj.IsSetItemType)
	{
    	    s += " AND ItemType = @ItemType ";
	}
	if (obj.IsSetItemObject)
	{
    	    s += " AND ItemObject = @ItemObject ";
	}
	if (obj.IsSetItemNotes)
	{
    	    s += " AND ItemNotes = @ItemNotes ";
	}
            if (s.Length > 0)
            {
                s =" WHERE "+s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, QT_ITEMINOUT obj)
        {
	    if (obj == null) { return;}
            	            if (obj.IsSetItemID)
            {
            	DalTools.addDbDataParameter(command, "ItemID", obj.ItemID, DbType.Int32); 
            }
            if (obj.IsSetItemName)
            {
            	DalTools.addDbDataParameter(command, "ItemName", obj.ItemName, DbType.String); 
            }
            if (obj.IsSetItemType)
            {
            	DalTools.addDbDataParameter(command, "ItemType", obj.ItemType, DbType.String); 
            }
            if (obj.IsSetItemObject)
            {
            	DalTools.addDbDataParameter(command, "ItemObject", obj.ItemObject, DbType.String); 
            }
            if (obj.IsSetItemNotes)
            {
            	DalTools.addDbDataParameter(command, "ItemNotes", obj.ItemNotes, DbType.String); 
            }
        }
        #endregion
    }
}